// pages/food/food.js

let app = getApp()
let config = require('../../utils/config.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    host: app.globalData.host
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.requestData();
    // console.log(app.globalData.host)
    // console.log(config.host)
  },

  /**
   * 
   */
  requestData: function() {
    // 这里注意this的指向
    let that = this;

    wx.request({
      url: app.globalData.host + 'api/productlist',
      data: '',
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        // console.log(res);

        let result = res.data.result;
        for (let i = 0; i < result.length; i++) {
          for (let j = 0; j < result[i].list.length; j++) {
            result[i].list[j].img_url = result[i].list[j].img_url.replace(/\\/g, '/');
          }
        }

        // 这里注意this的指向
        that.setData({
          list: result
        })
      },
      fail: function(res) {
        console.log(res)
      },
      complete: function(res) {},
    })
  },

  /**
   * 
   */
  goFoodContent: function(e) {
    let id = e.currentTarget.dataset.id;
    // console.log(id);
    wx.navigateTo({
      url: '../foodcontent/foodcontent?id=' + id
    });
  },

})